//
//  HomeTableViewCell.swift
//  WB
//
//  Created by leador on 17/5/10.
//  Copyright © 2017年 袁满. All rights reserved.
//

import UIKit
import SDWebImage
import HYLabel

private let edgeMargin:CGFloat = 15
private let itemMargin:CGFloat = 10

class HomeTableViewCell: UITableViewCell {

    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var verifiedView: UIImageView!
    @IBOutlet weak var screenNameLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: HYLabel!
    @IBOutlet weak var vipView: UIImageView!
    @IBOutlet weak var picCollectionView: PicCollectionView!
    @IBOutlet weak var retweetedContentLabel: HYLabel!
    @IBOutlet weak var retweetedBgView: UIView!
    @IBOutlet weak var bottomToolView: UIView!
    
    //MARK: ---约束的属性---
    @IBOutlet weak var contentLabelWidthCons: NSLayoutConstraint!
    @IBOutlet weak var picViewWCons: NSLayoutConstraint!
    @IBOutlet weak var picViewHCons: NSLayoutConstraint!
    @IBOutlet weak var picViewBottomCons: NSLayoutConstraint!
    @IBOutlet weak var retweetedContentTopCons: NSLayoutConstraint!
    
    //MARK: ---自定义属性---
    var viewModel:StatusViewModel? {
        
        didSet {
            
            guard let viewModel = viewModel else {
                return
            }
            
            iconView.sd_setImage(with: viewModel.profileUrl, placeholderImage:#imageLiteral(resourceName: "avatar_default"))
            verifiedView.image = viewModel.verifiedImage
            screenNameLabel.text = viewModel.status?.user?.screen_name
            timeLabel.text = viewModel.dateText
            contentLabel.attributedText = FindEmoticon.shareInstance.findEmmticon(status: viewModel.status?.text, font: contentLabel.font)
            vipView.image = viewModel.vipImage
            screenNameLabel.textColor = viewModel.vipImage==nil ? UIColor.black : UIColor.orange
            
            if let sourceText = viewModel.sourceText {
                sourceLabel.text = "来自\(sourceText)"
            }else {
                sourceLabel.text = nil
            }
            
            let picViewSize = calculatePicSize(count: viewModel.picURLs.count)
            picViewWCons.constant = picViewSize.width
            picViewHCons.constant = picViewSize.height
            picCollectionView.picURLs = viewModel.picURLs
            
            if viewModel.status?.retweeted_status != nil {
                
                if let screenName = viewModel.status?.retweeted_status?.user?.screen_name,
                    let retweetContent = viewModel.status?.retweeted_status?.text {
                    
                    let str = "@\(screenName):\(retweetContent)"
                
                    retweetedContentLabel.attributedText = FindEmoticon.shareInstance.findEmmticon(status: str, font: retweetedContentLabel.font)
                    
                }
                
                retweetedBgView.isHidden = false
                retweetedContentTopCons.constant = 15
            
            }else {
                
                retweetedContentLabel.text = nil
                retweetedBgView.isHidden = true
                retweetedContentTopCons.constant = 0
            }
            
            layoutIfNeeded()
            
            viewModel.cellHeight = bottomToolView.frame.maxY
            
        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentLabelWidthCons.constant = kScreenWidth - 2*edgeMargin
        
        // 监听@谁谁谁的点击
        contentLabel.userTapHandler = { (label, user, range) in
            print(label)
            print(user)
            print(range)
        }
        
        // 监听链接的点击
        contentLabel.linkTapHandler = { (label, link, range) in
            print(label)
            print(link)
            print(range)
        }
        
        // 监听话题的点击
        contentLabel.topicTapHandler = { (label, topic, range) in
            print(label)
            print(topic)
            print(range)
        }
        
    }

}

extension HomeTableViewCell {
    
    func calculatePicSize(count:Int)->CGSize {
        
        if count == 0 {
            picViewBottomCons.constant = 0
            return CGSize.zero
            
        }
        
        picViewBottomCons.constant = 10
        
        let layout = picCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        let imageWH = (kScreenWidth - 2*edgeMargin - 2*itemMargin) / 3
        
        if count == 1 {
         
            let im = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: viewModel?.picURLs.first?.absoluteString)
            
            layout.itemSize = (im?.size ?? CGSize(width:imageWH,height:imageWH))
            
            return layout.itemSize
            
        }else {
            
            layout.itemSize = CGSize(width:imageWH,height:imageWH)
        }
    
        if count == 4 {
            
            let picWH = imageWH * 2 + itemMargin
            
            return CGSize(width:picWH,height:picWH)
            
        }
        
        let rows = CGFloat((count - 1) / 3 + 1)
        
        let picW = kScreenWidth - 2 * edgeMargin
        
        let picH = rows * imageWH + (rows-1) * itemMargin
        
        return CGSize(width:picW,height:picH)
        
    }
    
}
